<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 4);

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/team_details.php");
include_once("includes/addMessage.php");

$done = false;
// are we showing an actual profile?
if(isset($_GET['id']))
{
	// Create query
	$q = "SELECT * FROM `". TEAMS_TABLE ."` "
		."WHERE t_id = ". $_GET['id'];
			
	
	// Run query
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$style = new style(true);	
		include_once('includes/side_menu.php');
		$style->addRenderLeft(new side_menu('teams', $_GET['id']));
		if(isset($_GET['mode']))
		{
			$team = new team_details($row, $_GET['mode']);
		}
		else
		{
			$team = new team_details($row, 'information');
		}
		$style->addRenderRight($team);
		$style->render();
		$done = true;
	}
}

if(!$done)
{
	include_once('includes/renderInclude.php');

	if(isset($_GET['p']))
	{
		$page = $_GET['p'];
		$start =($page * 20) - 20;
	}
	else
	{
		$page = 1;
		$start = 0;
	}
	$text = $lang['TeamsText'] . "<br><br>";
	
	if(isset($_GET['all']))
	{
		// Create query
		$q = "SELECT *, (select group_concat(pf_drivername SEPARATOR ', ') from " . TEAMMEMBERS_TABLE . ', ' . DRIVERNAME_TABLE .
						 " where tm_team = t.t_id AND tm_approved = 1 AND tm_driver = user_id) AS t_drivers,
						 (select group_concat(s.t_name SEPARATOR ', ') from " . TEAMS_TABLE . " s
						   where s.t_parent =t. t_id) AS t_satteams 
			  FROM ". TEAMS_TABLE . " t
			  WHERE t.t_parent = 0 
			  ORDER BY t_name ASC LIMIT " . $start . ', 20';
		$text .= $lang['ViewButton'] . ": <a href='teams.php'>" . $lang['TeamsActive'] . "</a> | " . $lang['TeamsAll'] . '<br>' . $lang['Page'] . ': ';
		
		$title = $lang['TeamsAll'];
		$url_action = 'teams.php?all=true&p=';
		$qC = 'SELECT COUNT(*) as row_count FROM ' . TEAMS_TABLE . ' WHERE t_parent = 0';
	}
	else
	{
		// Create query
		$q = "SELECT *, (select group_concat(pf_drivername SEPARATOR ', ') from " . TEAMMEMBERS_TABLE . ', ' . DRIVERNAME_TABLE .
						 " where tm_team = t.t_id AND tm_approved = 1 AND tm_driver = user_id) AS t_drivers,
						 (select group_concat(s.t_name SEPARATOR ', ') from " . TEAMS_TABLE . " s
						   where s.t_parent =t. t_id) AS t_satteams 
			  FROM ". TEAMS_TABLE . " t
			  WHERE t.t_parent = 0 
			  HAVING  t_drivers IS NOT NULL
			  ORDER BY t.t_name ASC LIMIT " . $start . ', 20';
		$text .= $lang['ViewButton'] . ": " . $lang['TeamsActive'] . " | <a href='teams.php?all=true'>" . $lang['TeamsAll'] . "</a><br>" . $lang['Page'] . ': ';
		$title = $lang['TeamsActive'];
		$url_action = 'teams.php?p=';
		$qC = 'SELECT COUNT(distinct t_id) as row_count FROM ' . TEAMS_TABLE . ', ' . TEAMMEMBERS_TABLE . ' 
			   WHERE tm_team = t_id AND  t_parent = 0';
	}

	$res = $db->sql_query($qC);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$row['row_count'] = ceil($row['row_count'] / 20);
		for($x = 1; $x <= $row['row_count']; $x++)
		{
			if($page == $x)
			{
				$text .= $x . " | ";
			}
			else
			{
				$text .= "<a href='" . $url_action . $x . "'>" . $x . "</a> | ";
			}
		}
		
		if($text != '')
		{
			$text = substr($text, 0 , -2);
		}
	}
		
	$style = new style(false);				
	$style->addRenderLeft(new addMessage($text, $title));
	// Run query
	$res = $db->sql_query($q);
	$style->addRenderLeft(new renderInclude('tpl/tableStart.tpl'));
	while ( $row = $db->sql_fetchrow($res)  )
	{
		$style->addRenderLeft(new team_details($row, 'Mini'));
	}
	$style->addRenderLeft(new renderInclude('tpl/tableEnd.tpl'));
	$style->render();
}


echo $db->num_queries;
?>